package com.lzw.controller.superAdmin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzw.anno.Log;
import com.lzw.pojo.SuperAdmins;
import com.lzw.service.superAdmin.SuperManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin(origins = "http://localhost:7070")
@RestController
@RequestMapping("/superManage")
public class SuperManage {

    @Autowired
    private SuperManageService superManageService;

    //根据条件查询
    @GetMapping("/list")
    public PageInfo<SuperAdmins> list(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            String name, String phone){
        PageHelper.startPage(pageNum,pageSize);
        List<SuperAdmins> list = superManageService.list(pageNum, pageSize, name, phone);
        return new PageInfo<>(list);
    }

    //批量删除
    @Log
    @DeleteMapping("/delete/{ids}")
    public String delete(@PathVariable List<Integer> ids){
        superManageService.delete(ids);
        return "删除成功";
    }

    //添加
    @Log
    @PostMapping("/add")
    public ResponseEntity<String> add(@RequestBody SuperAdmins superAdmins) {
        try {
            superManageService.add(superAdmins);
            return ResponseEntity.ok("添加成功");
        } catch (IllegalArgumentException e) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
        }
    }

    //更新
    @Log
    @PutMapping("/update")
    public ResponseEntity<String> update(@RequestBody SuperAdmins superAdmins) {
        try {
            superManageService.update(superAdmins);
            return ResponseEntity.ok("更新成功");
        } catch (IllegalArgumentException e) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
        }
    }
}
